﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Phoenix.Backend
{
    public partial class ProfileInfo : System.Web.UI.Page
    {
        protected DataTable dt;
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'>" + "Cập nhật thông tin thành công." + "</p></div>";
        string success_changepass = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'>" + "Thay đổi mật khẩu thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        string info = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'>" + "Thông tin chưa được cập nhật." + "</p></div><br  class='clear'/>";
        string info_changepass = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'>" + "Mật khẩu chưa được cập nhật." + "</p></div><br  class='clear'/>";
        Phoenix.AppCode.Entities.Users users = new Phoenix.AppCode.Entities.Users();
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.UsersService.UsersList_GetByID(Convert.ToInt32(Session["ID_UserName"].ToString()));
                    txtUserNameE.Text = dt_Edit.Rows[0]["UserName"].ToString();
                    txtFullNameE.Text = dt_Edit.Rows[0]["FullName"].ToString();
                    txtPositionE.Text = dt_Edit.Rows[0]["Position"].ToString();
                    txtEmailE.Text = dt_Edit.Rows[0]["Email"].ToString();
                    txtPhoneE.Text = dt_Edit.Rows[0]["Phone"].ToString();
                    txtCreateDate.Text = dt_Edit.Rows[0]["CreateDate"].ToString();
                    txtLastLogin.Text = dt_Edit.Rows[0]["LastLogin"].ToString();
                }
                catch (FormatException)
                {
                    Response.Redirect("Error.aspx");
                }
            }
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            try
            {
                users.ID_Users = Session["ID_UserName"].ToString();
                users.Pass = txtPassNew.Text;
                
                Phoenix.AppCode.Bussiness.UsersService.ChangePassword(users);

                ClearAllFields();
                lblWarning.Text = success_changepass;
                //Response.Redirect("Users.aspx?Action=Create");
            }
            catch
            {
                lblWarning.Text = error;
                //Response.Redirect("Users.aspx?Action=Create");
            }
        }
        protected void btnCancelC_Click(object sender, EventArgs e)
        {
            ClearAllFields();
            lblWarning.Text = info_changepass;
        }

        private void ClearAllFields()
        {
            txtPassCurrent.Text = "";
            txtPassNew.Text = "";
            txtPassNewConfirm.Text = "";
        }


        protected void btnEdit_Click(object sender, EventArgs e)
        {

            users = Phoenix.AppCode.Bussiness.UsersService.GetUsersByID(Convert.ToInt32(Session["ID_UserName"].ToString()));
            users.FullName = txtFullNameE.Text;
            users.Position = txtPositionE.Text;
            users.Email = txtEmailE.Text;
            users.Phone = txtPhoneE.Text;
            Phoenix.AppCode.Bussiness.UsersService.EditProfile(users);
            lblWarningProfile.Text = success;
            //Response.Redirect("ProfileInfo.aspx?ID=" + Convert.ToInt32(Session["ID_UserName"].ToString()) + "");

        }
        protected void btnCancelE_Click(object sender, EventArgs e)
        {
            DataTable dt_Edit = Phoenix.AppCode.Bussiness.UsersService.UsersList_GetByID(Convert.ToInt32(Session["ID_UserName"].ToString()));
            txtUserNameE.Text = dt_Edit.Rows[0]["UserName"].ToString();
            txtFullNameE.Text = dt_Edit.Rows[0]["FullName"].ToString();
            txtPositionE.Text = dt_Edit.Rows[0]["Position"].ToString();
            txtEmailE.Text = dt_Edit.Rows[0]["Email"].ToString();
            txtPhoneE.Text = dt_Edit.Rows[0]["Phone"].ToString();
            txtCreateDate.Text = dt_Edit.Rows[0]["CreateDate"].ToString();
            txtLastLogin.Text = dt_Edit.Rows[0]["LastLogin"].ToString();
            lblWarningProfile.Text = info;
        }
    }
}